package uoc.biblioteca.struts.action;

import java.io.File;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DownloadAction;

public class ExampleFileDownload extends DownloadAction {

	
	  protected StreamInfo getStreamInfo(ActionMapping mapping, 
              ActionForm form,
              HttpServletRequest request, 
              HttpServletResponse response)
				throws Exception {
				
		  			System.out.println("ENTRA EN ExampleFileDownload");
		  
					// File Name
					//String fileName = mapping.getParameter();
					
		  			if(request.getSession().getAttribute("fileNameExport")!=null){
		  				
		  				String fileName = (String)request.getSession().getAttribute("fileNameExport");
		  				String filePath =(String) request.getSession().getAttribute("pathExport");
						
						System.out.println("ENTRA EN "+filePath+"/"+fileName);
						
						// Set the content disposition
						response.setHeader("Content-disposition","attachment; filename=" + fileName);
						
						// Download a "pdf" file - gets the file name from the
						// Action Mapping's parameter
						String contentType = "application/txt";
						
						File file = new File(filePath+"/"+fileName);
						
						return new FileStreamInfo(contentType, file);
		  				
		  			}
		  			
		  			else{
		  				
		  				String fileName = (String)request.getParameter("fileName");
		  				String filePath =(String) request.getSession().getAttribute("pathExport");
						
						System.out.println("ENTRA EN "+filePath+"/"+fileName);
						
						// Set the content disposition
						response.setHeader("Content-disposition","attachment; filename=" + fileName);
						
						// Download a "pdf" file - gets the file name from the
						// Action Mapping's parameter
						String contentType = "application/txt";
						
						File file = new File(filePath+"/"+fileName);
						
						return new FileStreamInfo(contentType, file);
		  				
		  			}

	  		}
	
}
